package model;

import view.GameFrame;

import java.util.Random;

public class Model {

    public GameFrame gameFrame;

    public Model(GameFrame gameFrame) {
        this.gameFrame = gameFrame;
    }

    public static int[][] data = new int[4][4];
    public static int x;
    public static int y;

    public static String images = "images\\";
    public static String choose = "Figure\\figure";
    public static String num = "1\\";
    //输赢判断
    public static boolean victory;
    Random random = new Random();

    public void initData() {
        //一维数组存储图片的序号
        int[] temp = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0};
        //打乱图片的序号
        for (int i = 0; i < temp.length; i++) {
            int index = random.nextInt(temp.length);
            int cache = temp[i];
            temp[i] = temp[index];
            temp[index] = cache;
        }
        //将打乱了图片序号的一维数组存入二维数组中
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                int index = i * 4 + j;
                //记录空白版的位置
                if (temp[index] == 0) {
                    x = i;
                    y = j;
                }
                data[i][j] = temp[index];
            }
        }
    }
    public void isVictory() {
        if (victory) {
            return;
        }
        for (int i = 1; i < 16; i++) {
            //通过循环看数组里的数据是否按顺序
            if (data[i / 4][i % 4] != i) {
                return;
            }
        }
        victory = true;
    }
    public void reStart() {
        victory = false;
        //销毁窗体
        gameFrame.dispose();
        initData();
      new GameFrame().initImg();
    }
}
